package de.lulusoft.anothertorrent.gui.util;

import java.text.DecimalFormat;

public class Util {

	/**
	 * Formats a Long value to the corresponding b, mb.. 
	 * Uses two trailing numbers..
	 * @param bytes
	 * @return
	 */
	public static String formatBytes(Long bytes)
	{
		if (bytes == 0) 
			return "";
	    String[] sizes = {"B","KB","MB","GB","TB","PB"};
	    DecimalFormat format = new DecimalFormat("#.##");
	    int selected = 0;
	    Double d = (double)bytes;
	    while (d >= 1024) 
	    {
	        selected++;
	        d = d / 1024;
	    }
	    return format.format(d) + " " + sizes[selected];
	}
}
